//
//  ViewController.m
//  14-CollectionViewLiWuShuoHomeWork
//
//  Created by 曹曹 on 16/8/2.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "LabelButtonCollectionReusableView.h"
#import "ImageLabelCollectionReusableView.h"

#define kCollectionVeiwCellReuse @"reuseCell"
#define kCollectionViewHeaderReuse @"header"
#define kCollectionViewHeaderCollectionView @"headerCollection"
#define kCollectionViewSearchLabelImageReuse @"searchLabelImageResue"

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSegmentControl];
    [self createCollectionView];
}

- (void)createCollectionView {
    // init flowLayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 30) / 2, 100);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(0, 50);
    // init CollectionView
    UICollectionView *collectionVeiw = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionVeiw.backgroundColor = [UIColor whiteColor];
    // Delegate and Datasource
    collectionVeiw.delegate = self;
    collectionVeiw.dataSource = self;
    collectionVeiw.showsVerticalScrollIndicator = NO;
    [self.view addSubview:collectionVeiw];
    // register cell
    [collectionVeiw registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionVeiwCellReuse];
    // cell's header
    [collectionVeiw registerClass:[LabelButtonCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderReuse];
    // another header
    [collectionVeiw registerClass:[ImageLabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCollectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        // 返回补充视图
        // 补充视图占据整个第0分区
        ImageLabelCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCollectionView forIndexPath:indexPath];
        return view;
    } else {
        // 返回第1分区cell的补充视图(头视图)
        LabelButtonCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderReuse forIndexPath:indexPath];
        return view;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        // 补充视图整个的高度为300
        // 对于整个大的CollectionView来说,滚动方向是竖直的,因此只需要设置视图的高即可
        return CGSizeMake(0, 300);
    } else {
        // 注册的cell的头视图的高度为50
        return CGSizeMake(0, 50);
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        // 在第一个分区中只注册了一个补充视图(SupplementaryElement)也就是头视图
        // 在补充视图中添加了各种控件和一个横向的collectionView
        // 所以不能返回item个数,给0就可以了
        return 0;
    } else {
        return 30;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionVeiwCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)createSegmentControl {
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"单品"]];
    segment.frame = CGRectMake(0, 0, 100, 30);
    segment.tintColor = [UIColor whiteColor];
    segment.selectedSegmentIndex = 0;
    self.navigationItem.titleView = segment;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
